package com.sdu.rpc.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketRpcServer {

	private int port = 9999;

	private ExecutorService executorService = Executors.newFixedThreadPool(2);

	private boolean flag = true;

	public void startSocketServer() {
		ServerSocket server = null;
		try {
			server = new ServerSocket(port);

			while (flag) {
				Socket socket = server.accept();
				ServiceHandler serviceHandler = new ServiceHandler(socket);
				executorService.execute(serviceHandler);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			executorService.shutdown();

			if (server != null) {
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
